/**
 * Project Name:Micro-Services-Common
 * File Name   :MethodInvokeUtils.java
 * Package Name:com.micro.services.common.utils
 * Date:2015-9-9上午9:28:55
 * Copyright (c) 2015, http://my.oschina.net/httpssl All Rights Reserved.
 *
 */

package com.micro.services.common.utils;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

import org.apache.commons.lang3.reflect.MethodUtils;

/**
 * ClassName:MethodInvokeUtils <br/>
 * Function :TODO ADD FUNCTION. <br/>
 * Reason :TODO ADD REASON. <br/>
 * Date :2015-9-9 上午9:28:55 <br/>
 * 
 * @author :http://my.oschina.net/httpssl
 * @email :491835898@QQ.COM
 * @since :JDK 1.7
 * @see
 */
public class MethodInvokeUtils implements InvocationHandler
{

	private final Object	target;
	private final String	methodName;

	public MethodInvokeUtils(Object target, String methodName)
	{
		super();
		this.target = target;
		this.methodName = methodName;
	}

	@Override
	public Object invoke(Object proxy, Method method, Object[] parameters) throws Throwable {
		if (hasMatchingParametersMethod(method)) {
			return MethodUtils.invokeMethod(this.target, this.methodName, parameters);
		}
		return MethodUtils.invokeMethod(this.target, this.methodName, new Object[0]);
	}

	private boolean hasMatchingParametersMethod(Method method) {
		return (MethodUtils.getAccessibleMethod(this.target.getClass(), this.methodName,
				method.getParameterTypes()) != null);
	}

}
